﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using DNA.OpenAPI.OpenSearch;
using DNA.Mvc.Searching;
using System.Web.Security;

namespace DNA.Mvc.Controllers
{
    public class SearchController : Controller
    {
        private WebSiteContext _context;
        private IEnumerable<OpenSearchProvider> _providers = null;

        public SearchController(WebSiteContext websitecontext)
        {
            _context = websitecontext;
        }

        [SiteMapAction(Title = "Search",
            ShowInMenu = false,
            IsShared = true,
            IgnoreRouteDataKeys = new string[] { "terms", "provider", "index", "size" })]
        public ActionResult Search(string terms, string provider, int? index = 1, int? size = 20)
        {
            ViewBag.SearchTerms = terms;
            ViewBag.ProviderName = provider;
            var searcher = new SearchEngine(Providers);
            return View(searcher.SearchAll(terms, provider, index.Value, size.Value));
        }

        public JsonResult Suggest(string term, int? count = 20)
        {
            if (!string.IsNullOrEmpty(term))
            {
                var searcher = new SearchEngine(Providers);
                var result = searcher.Suggest(term, count).Select(s => s.SearchTerms).ToArray();
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(new { }, JsonRequestBehavior.AllowGet);
        }

        private IEnumerable<OpenSearchProvider> Providers
        {
            get
            {
                if (_providers == null)
                    _providers = DependencyResolver.Current.GetServices<OpenSearchProvider>();
                return _providers;
            }
        }

        public ActionResult Osd()
        {
            var web = _context.Web;
            var domain = Request.Url.Authority;
            var osdBuilder = new OsdBuilder(domain, "Search " + domain + " content");
            osdBuilder.ContactInfo(Membership.GetUser(web.Owner).Email, "dotnetage.com development team");

            osdBuilder.AddIcon(web.ShortcutIconUrl);
            string baseUrl = Request.Url.Scheme + "://" + domain;
            osdBuilder.AddSearchUrl(baseUrl + "/Search?terms={searchTerms}", "text/html");

            //Add suggession
            osdBuilder.AddSearchUrl(baseUrl + "/" + Url.Action("Suggest", "Search", new { Area = "" ,website=web.Name}) + "?terms={searchTerms}", "application/x-suggestions+json");
            osdBuilder.EnableClientPlugin(baseUrl + "/" + Url.Action("Osd", "Search", new { Area = "", website = web.Name }));
            return Content(osdBuilder.GetXml(), "text/xml");
        }
    }
}
